﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CarolLib.PostCrawler.Model
{
    /// <summary>
    /// 类t_Type。
    /// </summary>
    [Serializable]
    public partial class TType
    {
        public TType()
        { }

       	/// <summary>
		/// Type OR映射构造函数
		/// </summary>
		public TType(System.Data.IDataReader dr)
		{
			this.TypeID = (Guid)dr["TypeID"];
			this.ThreadID = (Guid)dr["ThreadID"];
			this.TypeNo = Convert.ToInt32(dr["TypeNo"]);
			this.TypeName = Convert.ToString(dr["TypeName"]);
		}
        
		#region Model
		private Guid _typeid;
		private Guid _threadid;
		private int _typeno;
		private string _typename;
		/// <summary>
		/// 
		/// </summary>
		public Guid TypeID
		{
			set{ _typeid=value;}
			get{return _typeid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public Guid ThreadID
		{
			set{ _threadid=value;}
			get{return _threadid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int TypeNo
		{
			set{ _typeno=value;}
			get{return _typeno;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string TypeName
		{
			set{ _typename=value;}
			get{return _typename;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public TType(Guid TypeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select TypeID,ThreadID,TypeNo,TypeName ");
			strSql.Append(" FROM [t_Type] ");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier)};
			parameters[0].Value = TypeID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["TypeID"]!=null && ds.Tables[0].Rows[0]["TypeID"].ToString()!="")
				{
					this.TypeID=Guid.Parse(ds.Tables[0].Rows[0]["TypeID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ThreadID"]!=null && ds.Tables[0].Rows[0]["ThreadID"].ToString()!="")
				{
					this.ThreadID=Guid.Parse(ds.Tables[0].Rows[0]["ThreadID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TypeNo"]!=null && ds.Tables[0].Rows[0]["TypeNo"].ToString()!="")
				{
					this.TypeNo=int.Parse(ds.Tables[0].Rows[0]["TypeNo"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TypeName"]!=null && ds.Tables[0].Rows[0]["TypeName"].ToString()!="")
				{
					this.TypeName=ds.Tables[0].Rows[0]["TypeName"].ToString();
				}
			}
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid TypeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [t_Type]");
			strSql.Append(" where TypeID=@TypeID ");

			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier)};
			parameters[0].Value = TypeID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [t_Type] (");
			strSql.Append("TypeID,ThreadID,TypeNo,TypeName)");
			strSql.Append(" values (");
			strSql.Append("@TypeID,@ThreadID,@TypeNo,@TypeName)");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@TypeNo", SqlDbType.Int,4),
					new SqlParameter("@TypeName", SqlDbType.NVarChar,50)};
			parameters[0].Value = TypeID;
			parameters[1].Value = ThreadID;
			parameters[2].Value = TypeNo;
			parameters[3].Value = TypeName;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [t_Type] set ");
			strSql.Append("TypeID=@TypeID,");
			strSql.Append("ThreadID=@ThreadID,");
			strSql.Append("TypeNo=@TypeNo,");
			strSql.Append("TypeName=@TypeName");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@TypeNo", SqlDbType.Int,4),
					new SqlParameter("@TypeName", SqlDbType.NVarChar,50)};
			parameters[0].Value = TypeID;
			parameters[1].Value = ThreadID;
			parameters[2].Value = TypeNo;
			parameters[3].Value = TypeName;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid TypeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [t_Type] ");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier)};
			parameters[0].Value = TypeID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(Guid TypeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select TypeID,ThreadID,TypeNo,TypeName ");
			strSql.Append(" FROM [t_Type] ");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier)};
			parameters[0].Value = TypeID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["TypeID"]!=null && ds.Tables[0].Rows[0]["TypeID"].ToString()!="")
				{
					this.TypeID=new Guid(ds.Tables[0].Rows[0]["TypeID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ThreadID"]!=null && ds.Tables[0].Rows[0]["ThreadID"].ToString()!="")
				{
					this.ThreadID=new Guid(ds.Tables[0].Rows[0]["ThreadID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TypeNo"]!=null && ds.Tables[0].Rows[0]["TypeNo"].ToString()!="")
				{
					this.TypeNo=int.Parse(ds.Tables[0].Rows[0]["TypeNo"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TypeName"]!=null && ds.Tables[0].Rows[0]["TypeName"].ToString()!="")
				{
					this.TypeName=ds.Tables[0].Rows[0]["TypeName"].ToString();
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<TType> GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM [t_Type] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}

            using (var dr = DbHelperSQL.ExecuteReader(strSql.ToString()))
            {
                List<TType> result = new List<TType>();

                while (dr.Read())
                {
                    result.Add(new TType(dr));
                }

                dr.Close();
                return result;
            }
        }

        #endregion
    }
}